<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="Docker中SQlite的配置和挂载"/>




  <meta name="keywords" content="Docker, SQlite, 八一" />



  <meta name="baidu-site-verification" content="HhUstaSjr0" />



  <meta name="google-site-verification" content="UA-102975942-1" />






  <link rel="alternate" href="/atom.xml" title="八一">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="https://bay1.top/2018/03/13/Docker中SQlite的配置和挂载/"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />
<link rel="stylesheet" type="text/css" href="/css/prettify.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/sons-of-obsidian.css" media="screen" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  
  <script id="baidu_analytics">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?9a885cc9fb6cd7bcef579deb8efe8a70";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>



  <script id="google_analytics">
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-102975942-1', 'auto');
        ga('send', 'pageview');
  </script>










    <title> Docker中SQlite的配置和挂载 - 八一 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">八一</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/archives">
        <li class="mobile-menu-item">
          
          
            文章
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            标签
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            关于/友链
          
        </li>
      </a>
    
      <a href="/search">
        <li class="mobile-menu-item">
          
          
            站内搜索
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">八一</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives">
            
            
              文章
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              标签
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              关于/友链
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/search">
            
            
              站内搜索
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          Docker中SQlite的配置和挂载
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2018-03-13
        </span>
        
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#关于SQLite配置"><span class="toc-text">关于SQLite配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#关于SQlite挂载-备份"><span class="toc-text">关于SQlite挂载,备份</span></a></li></ol>
    </div>
  </div>


    <div class="post-content">
      
        <p>部署寒假里工作室的成果,二维码扫码登陆就暂停了,遇到了Docker对SQlite数据库的操作<a id="more"></a></p>
<blockquote>
<p>一开始写Demo都是用的Mysql,后来发现了SQlite,就开始变懒了…..<br>(虽然SQlite无需配置,用起来方便,但是真正的生产中还是要选择合适数据库进行储存数据)<br>现在喜欢上了用Docker,而关于Docker中如何配置SQlite的文章似乎很少<br>(只有像我这种练手的采用这种数据库吧…..)</p>
</blockquote>
<h2 id="关于SQLite配置"><a href="#关于SQLite配置" class="headerlink" title="关于SQLite配置"></a>关于SQLite配置</h2><p>在Dockerflie文件中,一开始采取的方式是一种很笨的方法<br>没有相关的SQlite配置说明(或者是我没找到)<br>就直接在文件中COPY过去,或者在Dockerfile中加入生成数据库文件的命令</p>
<figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">COPY</span><span class="bash"> data.sqlite /app/</span></span><br><span class="line"><span class="bash">RUN python manage.py createdb</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>如果直接进行COPY的话,会出现权限问题<br>导致镜像中sqlite文件不能被写入<br>在Dockerfile文件中加入chmod</p>
</blockquote>
<figure class="highlight docker"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">RUN</span><span class="bash"> chmod 644 data.sqlite</span></span><br></pre></td></tr></table></figure>
<h2 id="关于SQlite挂载-备份"><a href="#关于SQlite挂载-备份" class="headerlink" title="关于SQlite挂载,备份"></a>关于SQlite挂载,备份</h2><p>今天在部署的时候想到,这种文件储存数据的形式<br>等部署成功并且运行一段时间后,数据库的文件如何进行备份呢？</p>
<blockquote>
<p>一种则是从docker中cp出来数据库文件带宿主机</p>
</blockquote>
<figure class="highlight docker"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker cp 容器name：容器中文件路径 宿主机地址</span><br><span class="line">docker cp container /app/data/data.sqlite /home/bay1/data</span><br></pre></td></tr></table></figure>
<blockquote>
<p>另一种则是数据卷的形式<br>将sqlite文件挂在到宿主机,进行实时共享和保存<br>挑一个docker-compose文件的例子</p>
</blockquote>
<figure class="highlight docker"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">version: <span class="string">'2'</span></span><br><span class="line">services:</span><br><span class="line">  vue:</span><br><span class="line">    build: .</span><br><span class="line">    volumes:</span><br><span class="line">    - /home/wwwroot/vue/data:/vue/data</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">8004</span>:<span class="number">5000</span></span><br></pre></td></tr></table></figure>
<p>其中sqlite文件保存的目录是data,volumes指定相应的宿主机和容器路径<br>(宿主机要为绝对路径)</p>
<blockquote>
<p>数据卷的形式是可以直接挂载文件的,而不同于上述的目录<br>但是似乎只能通过命令行进行设定<br>(<a href="http://www.cnblogs.com/wade-luffy/p/6542539.html" target="_blank" rel="noopener">原链接</a>)</p>
</blockquote>
<figure class="highlight docker"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker <span class="keyword">run</span><span class="bash"> -it -v 宿主机文件路径:挂载到文件路径 镜像名 /bin/bash</span></span><br></pre></td></tr></table></figure>
<p>需要注意的是如果文件的inode改变的话,会出现报错信息<br>所以网上很多就直接推荐的方式是挂载文件所在的目录</p>
<p><a href="http://www.ruanyifeng.com/blog/2011/12/inode.html" target="_blank" rel="noopener">理解inode</a></p>

      
    </div>

    
      
      



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/Docker/">Docker</a>
            
              <a href="/tags/SQlite/">SQlite</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/2018/04/04/如何接入易班第三方登录/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">如何接入易班第三方登录</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
      <a class="next" href="/2018/03/11/WebSocket-Flask实现即时弹幕系统-实现微信扫码登录/">
        <span class="next-text nav-default">WebSocket+Flask实现即时弹幕系统--实现微信扫码登录</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div> 
    </div>
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="https://github.com/bay1" class="iconfont icon-github" title="github"></a>
        
      
    
      
        
          <a href="http://weibo.com/3190704711/profile?topnav=1&wvr=6&is_all=1" class="iconfont icon-weibo" title="weibo"></a>
        
      
    
      
    
      
    
      
    
    
    
  </div>


<div class="copyright">
  <span class="copyright-year">
    
    &copy; 
     
      2016 - 
    
    2018
    <span class="author">bay1</span>
  </span>
</div>
      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://bay1.top/2018/03/13/Docker中SQlite的配置和挂载/';
        this.page.identifier = '2018/03/13/Docker中SQlite的配置和挂载/';
        this.page.title = 'Docker中SQlite的配置和挂载';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//https-blog-flywinky-top-1.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>



    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>
<script src="/js/prettify.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $('pre').addClass('prettyprint');
   prettyPrint();
 })
</script>
  </body>
</html>
